package api

import (
	"net/http"
	"fmt"
	"io/ioutil"
	"encoding/json"
)

// var access_token string = "123.ce846f8ac91e39d79199f5b6144c8971.Ys1XeaRGLtIJ_6BYYv_c2JWvC9VaTQF7g3gI4AY.kFPMQA"

type  UserInfo struct{
	BaiduName string `json:"baidu_name"`
	VipType int `json:"vip_type"`
}

type ReturnFileList struct {
	List []FileInfo `json:"list"`
}

type FileInfo struct {
	FileName string `json:"server_filename"`
	IsDir int `json:"isdir"`
	FsId int64 `json:"fs_id"`
}

type FileDLinkList struct {
	List []FileDLink `json:"list"`
}

type FileDLink struct {
	//文件下载连接
	Dlink string `json:"dlink"`
}

func GetUserInfo() {

	resp, err := http.Get(fmt.Sprintf("https://pan.baidu.com/rest/2.0/xpan/nas?method=uinfo&access_token=%s", access_token))
	if err != nil {
		fmt.Println(err.Error())
		return 
	}
	defer resp.Body.Close()
	s,err:=ioutil.ReadAll(resp.Body)
	// fmt.Println(string(s))
	user := UserInfo{}
	err = json.Unmarshal(s, &user)
	// fmt.Printf("用户信息为%v", user)
	fmt.Println("用户名为：" + user.BaiduName)
	if user.VipType == 2 {
		fmt.Println("用户为超级会员")
	} else if user.VipType == 0 {
		fmt.Println("用户为普通用户")
	}
}

func ListFile(path string)  []FileInfo {
	resp, err := http.Get(fmt.Sprintf("https://pan.baidu.com/rest/2.0/xpan/file?method=list&dir=%s&start=0&limit=200&web=web&access_token=%s", path, access_token))
	if err != nil {
		fmt.Println(err.Error())
		return []FileInfo{}
	}
	defer resp.Body.Close()
    s,err:=ioutil.ReadAll(resp.Body)
	returnFileList := ReturnFileList{}
	err = json.Unmarshal(s, &returnFileList)
	if err != nil {
		fmt.Println(err.Error())
		return []FileInfo{}
	}
	// fmt.Println(returnFileList.List)
	return returnFileList.List
}

func LSCommand(path string) {
	fileList := ListFile(path)
	for _, fileObj := range fileList {
		isDir := "none"
		if fileObj.IsDir == 1 {
			isDir = "目录"
		} else {
			isDir = "文件"
		}
		fmt.Println(fileObj.FileName + " 类型:" + isDir)
	}
}

func ExistDir(path string, fileName string) bool {
	fileList := ListFile(path)
	for _, fileObj := range fileList {
		
		if fileObj.IsDir == 1 {
			if fileName == fileObj.FileName {
				return true
			}
		} 
	}
	return false
}

func GetUrl(fileName string, path string) string {
	//返回文件下载的url
	fileList := ListFile(path)
	for _, fileObj := range fileList {
		
		if fileObj.IsDir == 0 {
			// fmt.Println(fileObj.FileName)
			if fileName == fileObj.FileName {
				//说明找到需要下载的这个文件了。先去查找文件信息，然后找到下载连接。
				fileId := fileObj.FsId
				// fmt.Println(fileId)
				resp, err := http.Get(fmt.Sprintf("https://pan.baidu.com/rest/2.0/xpan/multimedia?method=filemetas&fsids=[%v]&access_token=%s&dlink=1", fileId, access_token))
				if err != nil {
					fmt.Println(err.Error())
					return ""
				}
				defer resp.Body.Close()
				s,err:=ioutil.ReadAll(resp.Body)
				// fmt.Println(string(s))
				fileDLinkList := FileDLinkList{}
				err = json.Unmarshal(s, &fileDLinkList)
				if err != nil {
					fmt.Println(err.Error())
					return ""
				}
				// fmt.Println(fileDLinkList)
				fileDlink := fileDLinkList.List[0] // 得到下载连接！
				dlink := fileDlink.Dlink
				return dlink + "&access_token=" + access_token 
			}
		} 
	}
	fmt.Println("无此文件!请重新输入文件名。")
	return ""
}